<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 3/21/2019
 * Time: 5:33 PM
 */

namespace p5\v2\db;


class SqlBuilder
{
    const CRLF = "\r\n";
    private $parts = [];
    private $queryLimits = "";
    private $orders = [];
    private $groups = [];
    private $limit;

    public function toSql()
    {
        /**
         * @var $part SqlTablePart
         */
        $sql = "select ";
        $partSize = count($this->parts);
        for ($i = 0; $i < $partSize; $i++) {
            $part = $this->parts[$i];
            $sql .= $i > 0 ? "," : "";
            $sql .= static::CRLF . $part->getSelectColumns();
        }
        for ($i = 0; $i < $partSize; $i++) {
            $part = $this->parts[$i];
            if ($i === 0) {
                $sql .= static::CRLF . " from " . $part->getFromTableName();
                continue;
            }
            $sql .= static::CRLF . $part->getJoinPart();
        }
        $wheres = [];
        for ($i = 0; $i < $partSize; $i++) {
            $part = $this->parts[$i];
            $where = $part->getConditionPart();
            if (!empty($where)) {
                array_push($wheres, $where);
            }
        }
        if (count($wheres) > 0) {
            $sql .= static::CRLF . " where " . implode(" and ", $wheres);
        }

        $sql .= static::CRLF . " ". $this->queryLimits. " ";

        if (count($this->groups) > 0) {
            $sql .= static::CRLF . " group by " . implode(",", $this->groups);
        }
        if (count($this->orders) > 0) {
            $sql .= static::CRLF . " order by " . implode(",", $this->orders);
        }
        if (!empty($this->limit)) {
            $sql .= static::CRLF . " limit " . $this->limit;
        }
        $partSize = count($this->parts);
        for ($i = 0; $i < $partSize; $i++) {
            $part = $this->parts[$i];
            if ($part->hasAlias()) {
                // 替换成别名
                $sql = str_replace(" {$part->getTableName()}.", " {$part->getTableAiasName()}.", $sql);
            }
        }

        return $sql;
    }

    /**
     * @param $part SqlTablePart
     */
    public function addPart($part)
    {
        foreach ($this->parts as $one) {
            if ($one->getSpaceName() === $part->getSpaceName()) {
                $this->log("SqlTablePart spaceName=" . $part->getSpaceName() . " 已经存在了，请不要重复添加");
                // 重复添加 $one
                return;
            }
        }
        array_push($this->parts, $part);
    }

    /**
     * @param $string
     */
    private function log($string)
    {
        // TODO
    }

    public function addOrderBy($orderBy)
    {
        $orderBy = str_replace("order by", "", $orderBy);
        if (empty(trim($orderBy))) {
            return;
        }
        array_push($this->orders, $orderBy);
    }

    public function addGroupBy($groupBy)
    {
        array_push($this->groups, $groupBy);
    }

    public function setQueryLimits($queryLimits)
    {
        $this->queryLimits = $queryLimits;
    }

    public function setLimit($limit)
    {
        $this->limit = $limit;
    }

}
